﻿<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>理工计协在线代码编辑器</title>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

  <script type="text/javascript" src="/js/jquery-1.10.2.min.js"></script>

  <link rel="stylesheet" type="text/css" href="default.css" />

  <script>
    var afterFunction;
    var nIntervalID = 0;
    var nIsEdit = 0;
    function setMessage(sTitle, sContent, afterClose) {
      p_msg_title.innerHTML = sTitle;
      p_msg_area.innerHTML = sContent;
      PublicMsg.style.display = "block";
      mask.style.display = "block";
      afterFunction = afterClose;
    }
    function closeMessage() {
      PublicMsg.style.display = "none";
      mask.style.display = "none";
      afterFunction();
      clearInterval(nIntervalID);
      return true;
    }
    function saveCode() {
      if (fileinfo.value == "") {
        setMessage("系统提示", "未选择文件！", function() { });
        nIntervalID = setInterval(closeMessage, 800);
        return;
      }
      setMessage("系统提示", "正在保存中……", function() { });
      $.ajax({
        url: "thj.asp",
        type: "post",
        data: {
          Action: "savefile",
          FileName: fileinfo.value,
          Text: code.value
        },
        success: function(data) {
          setMessage("系统提示", "保存成功！", function() { });
          nIntervalID = setInterval(closeMessage, 1000);
        },
        error: function(oXHR, sError, errorThrown) {
          var sErrorMsg = "";
          switch (sError) {
            case 'timeout':
              {
                setMessage("系统提示", "连接超时", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
            case 'abort':
              {
                setMessage("系统提示", "连接断开", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
            default:
              {
                setMessage("系统提示", "未知错误", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
          }
        }
      });
    }
    function createCode() {
      var oOL = document.getElementById("fileList");
      var oLi = document.createElement("li");
      //判断是否已经处于创建新文件的状态，相当于加锁
      if (nIsEdit == 1) return;
      //如果当前不处于创建文件状态，则准备进入创建文件状态
      //1、清空代码区
      code.value = "";
      //2、清空隐藏域中的文件名
      fileinfo.value = "";
      //3、清空“当前文件的显示”
      nowEdit.innerHTML = "";
      nIsEdit = 1;
      oLi.innerHTML = '<span id="editbox"><input type="text" value="" onkeypress="onBtnKeyPress(this)" /><a href="javascript:void(0);" onclick="onExitEdit();">[x]</a></span>';
      oOL.appendChild(oLi);
    }
    function onExitEdit() {
      nIsEdit = 0; //退出编辑状态
      removeNode("editbox");
    }
    function removeNode(sObj) {
      try {
        document.getElementById(sObj).removeNode(true);
      }
      catch (e) {
        document.getElementById(sObj).remove();
      }
    }
    function onBtnKeyPress(o) {
      var currentEvent = event;
      if (currentEvent.keyCode == 13) {
        checkInput(o);
      }
    }
    function checkInput(o) {
      if (o.value == "") {
        alert("请填写正确的内容");
        return;
      }
      createFile(o);
    }
    function createFile(o) {
      var fileName = o.value;
      if (nIsEdit == 0) return;
      setMessage("系统提示", "正在创建文件", function() { });
      $.ajax({
        url: "thj.asp",
        type: "post",
        data: {
          Action: "createfile",
          FileName: fileName
        },
        success: function(data) {
          if (data == 'error') {
            setMessage("系统提示", "无权限进行操作！", function() { window.location = "login.asp"; });
            nIntervalID = setInterval(closeMessage, 1000);
          }
          else {
            if (nIsEdit == 0) return;
            nIsEdit = 0;
            code.value = "";
            closeMessage();
            fileinfo.value = fileName;
            var oLi = document.createElement("li");
            oLi.innerHTML = "<a href='javascript:void(0);' onclick='loadfile(\"" + fileName + "\");'>" + fileName + "</a>";
            document.getElementById("fileList").appendChild(oLi);
            try {
              document.getElementById("editbox").removeNode(true);
            }
            catch (e) {
              document.getElementById("editbox").remove();
              nowEdit.innerHTML = fileName;
            }
            nowEdit.innerHTML = fileName;
          }
        },
        error: function(oXHR, sError, errorThrown) {
          var sErrorMsg = "";
          switch (sError) {
            case 'timeout':
              {
                setMessage("系统提示", "连接超时", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
            case 'abort':
              {
                setMessage("系统提示", "连接断开", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
            default:
              {
                setMessage("系统提示", "未知错误", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
          }
        }
      });
    }

    function loadfile(fname) {
      //判断当前是不是没有退出新建文件状态
      if (nIsEdit == 1) {
        try {
          document.getElementById("editbox").removeNode(true);
        }
        catch (e) {
          document.getElementById("editbox").remove();
        }
        finally {
          nIsEdit = 0;
          nowEdit.innerHTML = "";
          code.value = "";
          fileinfo.value = "";
        }
      }
      fileinfo.value = fname;
      nowEdit.innerHTML = fname;
      code.value = "";
      setMessage("系统提示", "正在读取中……", function() { });
      $.ajax({
        url: "thj.asp",
        type: "post",
        data: {
          Action: "getfile",
          FileName: fname
        },
        success: function(data) {
          if (data == 'error') {
            setMessage("系统提示", "无权限进行操作！", function() { window.location = "login.asp"; });
            nIntervalID = setInterval(closeMessage, 1000);
          }
          else {
            code.value = data;
            closeMessage();
          }
        },
        error: function(oXHR, sError, errorThrown) {
          var sErrorMsg = "";
          switch (sError) {
            case 'timeout':
              {
                setMessage("系统提示", "连接超时", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
            case 'abort':
              {
                setMessage("系统提示", "连接断开", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
            default:
              {
                setMessage("系统提示", "未知错误", function() { });
                nIntervalID = setInterval(closeMessage, 1000);
                break;
              }
          }
        }
      });
    }
    </script>

</head>
<body>
  <div class="leftbe">
    <%
        dim oFSO,oFolder,oFiles,oFile
        set oFSO=server.CreateObject("Scripting.FileSystemObject")
        set oFolder=oFSO.GetFolder(Server.MapPath("."))
        set oFiles=oFolder.Files
        Response.Write("<ol style='list-style-type :none' id='fileList'>")
        for each oFile in oFiles
            Response.Write("<li><a href='javascript:void(0);' onclick='loadfile(""" & oFile.Name & """);'>" & oFile.Name & "</a></li>")
        next
        %>
  </div>
  <div class="rightbe">
    <br />
    <span style="margin: 18px;">当前文件：<span id="nowEdit"></span></span>
    <div class="inedit">
      <textarea id="code" class="codearea"></textarea>
    </div>
    <input type="button" class="btn2" value="创建文件" onclick="createCode();" />
    <input type="button" class="btn1" value="保存文件" onclick="saveCode();" />
    <input type="hidden" value="" id="fileinfo" />
  </div>
  <div class="mask_div" id="mask">
    <div class="msgdiv" id="PublicMsg">
      <span class="msg_title" id="p_msg_title"></span><span class="close_text" onclick="return closeMessage();">
        <a href="javascript:void(0);">[关闭]</a> </span><span class="msg_content" id="p_msg_area">
        </span>
    </div>
  </div>
</body>
</html>